using System;
using System.Text;
using System.Threading;
using System.Collections.Generic;


namespace Foundations.Net.Pooling
{
    public class PooledObject<T>:IDisposable where T: class
    {
        T _object;
        AutoResetEvent _handle;
        ActivationDelegate<T> _activate;
        DeActivationDelegate<T> _deactivate;

        internal PooledObject(T Object, AutoResetEvent Handle, ActivationDelegate<T> Activate, DeActivationDelegate<T> DeActivate)
        {
            _object = Object;
            _handle = Handle;
            _activate = Activate;
            _deactivate = DeActivate;

            if (_activate != null)
                _activate(Instance);
        }

        public T Instance
        {
            get { return _object; }
        }

        #region IDisposable Members
        public void Dispose()
        {
            if(_deactivate != null)
                _deactivate(Instance);
            _object = null;
            _handle.Set();

        }
        #endregion
    }
}
